package com.woniuxy.yoga.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniuxy.yoga.admin.dao.mapper.AdminRoleMapper;
import com.woniuxy.yoga.admin.dao.model.AdminRole;
import com.woniuxy.yoga.admin.dao.model.RoleResource;
import com.woniuxy.yoga.admin.dao.mapper.RoleResourceMapper;
import com.woniuxy.yoga.admin.model.exception.AdminException;
import com.woniuxy.yoga.admin.model.exception.AdminExceptionCode;
import com.woniuxy.yoga.admin.service.RoleResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.yoga.commons.core.statedata.AdminState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色资源关联表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class RoleResourceServiceImpl extends ServiceImpl<RoleResourceMapper, RoleResource> implements RoleResourceService {

    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 查询角色资源权限
     * @param roleId
     * @return
     */
    @Override
    public List<RoleResource> selectResourceByRole(Integer roleId) {
        //查询角色是否存在
        AdminRole adminRole = adminRoleMapper.selectById(roleId);
        if (adminRole==null) {
            throw new AdminException(AdminExceptionCode.ADMIN_EXCEPTION_CODE_ROLE_NOTEXISTS);
        }
        //开始查询关联的资源
        LambdaQueryWrapper<RoleResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResource::getRoleId,roleId).
                eq(RoleResource::getRelationState, AdminState.ROLE_RELATION_STATE_RIGHT);
        List<RoleResource> roleResources = roleResourceMapper.selectList(queryWrapper);
        return roleResources;
    }
}
